/*     */ package com.mbl.phone.realm;
/*     */ import java.sql.Timestamp;
/*     */ import java.time.LocalDateTime;

/*     */ import javax.annotation.Resource;

/*     */ import org.apache.shiro.SecurityUtils;
/*     */ import org.apache.shiro.authc.AuthenticationException;
/*     */ import org.apache.shiro.authc.AuthenticationInfo;
/*     */ import org.apache.shiro.authc.AuthenticationToken;
/*     */ import org.apache.shiro.authc.LockedAccountException;
/*     */ import org.apache.shiro.authc.SimpleAuthenticationInfo;
/*     */ import org.apache.shiro.authc.UnknownAccountException;
/*     */ import org.apache.shiro.authc.UsernamePasswordToken;
/*     */ import org.apache.shiro.authz.AuthorizationInfo;
/*     */ import org.apache.shiro.crypto.hash.SimpleHash;
/*     */ import org.apache.shiro.realm.AuthorizingRealm;
/*     */ import org.apache.shiro.session.Session;
/*     */ import org.apache.shiro.subject.PrincipalCollection;
/*     */ import org.apache.shiro.util.ByteSource;
/*     */ import org.slf4j.Logger;
/*     */ import org.slf4j.LoggerFactory;

/*     */ 
/*     */ import com.mbl.phone.pojo.User;
/*     */ import com.mbl.phone.service.UserService;
/*     */ import com.mbl.phone.utility.ResultUtil;
/*     */ 
/*     */ public class jdbcRealm extends AuthorizingRealm
/*     */ {
/*  31 */   static final Logger logger = LoggerFactory.getLogger(jdbcRealm.class);
/*     */   
/*     */   @Resource
/*     */   private UserService userService;
/*     */   @Resource
/*     */   private ResultUtil result;
/*     */   
/*     */   protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0)
/*     */   {
/*  40 */     return null;
/*     */   }
/*     */   
/*     */   protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException
/*     */   {
/*  45 */     User userLogin = tokenToUser((UsernamePasswordToken)token);
/*     */     
/*  47 */     User user = null;
/*     */     try {
/*  49 */       user = this.userService.selectTokenByUsername(userLogin.getUsername());
/*     */     }
/*     */     catch (Exception localException) {}
/*     */     
/*     */ 
/*  54 */     if (user == null) throw new UnknownAccountException();
/*  55 */     if (user.getStatus() != 0) { throw new LockedAccountException();
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*  60 */     Object principal = user.getUsername();
/*     */     
/*  62 */     Object credentials = user.getPassword();
/*     */     
/*  64 */     String realmName = getName();
/*     */     
/*  66 */     ByteSource credentialsSalt = ByteSource.Util.bytes(user.getNickname());
/*     */     
/*  68 */     SimpleAuthenticationInfo info = null;
/*     */     
/*  70 */     info = new SimpleAuthenticationInfo(principal, credentials, credentialsSalt, realmName);
/*     */     
/*     */ 
/*     */ 
/*  74 */     Session session = SecurityUtils.getSubject().getSession();
/*  75 */     user.setLast_login_time(new Timestamp(System.currentTimeMillis()));
/*  76 */     session.setAttribute("user", user);
/*  77 */     session.setAttribute("userId", user.getId());
/*     */     
/*  79 */     logger.info(user.getNickname() + "于" + LocalDateTime.now() + "登陆");
/*     */     
/*  81 */     return info;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static String getHashKey(String hashName, int hashNum, String sourcePass, String salt)
/*     */   {
/*  93 */     String hashAlgorithmName = hashName;
/*  94 */     Object credentials = sourcePass;
/*  95 */     ByteSource Salt = ByteSource.Util.bytes(salt);
/*     */     
/*  97 */     int hashIterations = hashNum;
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 105 */     Object result = new SimpleHash(hashAlgorithmName, credentials, Salt, hashIterations);
/* 106 */     return result.toString();
/*     */   }
/*     */   
/*     */   public static User tokenToUser(UsernamePasswordToken token) {
/* 110 */     User userLogin = new User();
/* 111 */     userLogin.setUsername(token.getUsername());
/* 112 */     userLogin.setPassword(String.valueOf(token.getPassword()));
/* 113 */     return userLogin;
/*     */   }
/*     */   
/*     */   public static void main(String[] args) {
/* 117 */     System.out.println(getHashKey("MD5", 2, "123456", "李良平"));
/*     */   }
/*     */ }


/* Location:              C:\Users\Administrator\Desktop\phone.war!\WEB-INF\classes\com\mbl\phone\realm\jdbcRealm.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       0.7.1
 */